$m(function() {
	var that = this;
	var $this = this.element;
	var pHeight = $this.parent().height();

	/**
	 * 初始化页面布局
	 */
	var initMainPage = function(){

		//初始化主页面布局
		initMainLayout();

		initSericeGrid($('#tempo_achieve_service_none_approve_tab'))
		$('#tempo_achieve_service_none_approve_tab').data('init', true);
	}
	
	/**
	 * 初始化主体布局
	 */
	var initMainLayout = function(){
		//主布局
		$('#tempo_service_approve').layout({
			north : {
				resizable : false,
				size : '100',
				spacing_open : 1,
				closable : false
			}
		})
		
		initQuery();
		$('#tempo_achieve_service_tabs').tabs({
			 activate: function( event, ui ) {
					var pid = ui.newPanel.attr('id');
					if(pid == 'tempo_achieve_service_none_approve'){
						if($('#tempo_achieve_service_none_approve_tab').data('init')){
							$('#queryForm').find('.queryBtn').trigger('click');
						}else{
							initSericeGrid($('#tempo_achieve_service_none_approve_tab'))
							$('#tempo_achieve_service_none_approve_tab').data('init', true);
						}
					}
					if(pid == 'tempo_achieve_service_has_approve'){
						if($('#tempo_achieve_service_has_approve_tab').data('init')){
							$('#queryForm').find('.queryBtn').trigger('click');
						}else{
							initSericeGrid2($('#tempo_achieve_service_has_approve_tab'))
							$('#tempo_achieve_service_has_approve_tab').data('init', true);
						}
					}
			 }
		});
	}
	
	
	var initQuery = function(){
		var form = $('#queryForm');
		TempoUtils.parserUi(form,{});
		form.find('[name="approveDate"]').datepicker({ 
			dateFormat : "yy-mm-dd",
			yearRange: "c-100:",
			changeYear:true,
			changeMonth:true,
			onChangeMonthYear : function(year, month, inst){ 
				var month = inst.selectedMonth + 1;
				var day = inst.selectedDay;
				var format = function(v) { return String(v).length > 1 ? v : ('0' + v) }
				$(this).val(inst.selectedYear + '-' + format(month) + '-' + format(inst.selectedDay));
			}
		});
		form.find('.queryBtn').button({click:function(){
			debugger
			var active = $("#tempo_achieve_service_tabs").tabs( "option", "active" );
			var serviceName = form.find('[name="serviceName"]').val();
			var projectName = form.find('[name="projectName"]').val();
			var themeName = form.find('[name="themeName"]').val();
			var approveUser = form.find('[name="approveUser"]').val();
			var approveDate = form.find('[name="approveDate"]').datepicker('getDate');
			
			var data = {
					serviceName: serviceName,
					projectName: projectName,
					themeName: themeName,
					approveUser: approveUser,
					approveDate: approveDate ? DateUtils.formatDate(approveDate, 'yyyy-MM-dd') :null,
					status : active == 0 ? null : active
			}
			if(active == 0 ){
				$('#tempo_achieve_service_none_approve_tab').datagrid('reloadGrid', data);
			}
			if(active == 1 ){
				$('#tempo_achieve_service_has_approve_tab').datagrid('reloadGrid', data);
			}
			return false;
		}});
		form.find('.resetBtn').button({click : function(){
			form[0].reset();
			form.find('[name="result"]').val('').selectmenu('refresh');
			return false;
		}});
	}
	
	//初始化服务列表
	var initSericeGrid = function(ele){
		var height = pHeight-155;
		ele.datagrid({
			datatype : "json",
			url : contextPath + '/api/ai/mine/publish/listApproveService?time=' + new Date().getTime(),
			height : height,
			fit:true,
			rownumbers : true,
			mtype : 'POST',
			autowidth:true,
			rownumbers : true,
			shrinkToFit : true, 
			viewrecords : true,
			striped : true,
			colNames : [ 'id', 
			             TempoUtils.locale("mine.publish-mine-service-grid-col-serviceid"),//'服务名称'
			             TempoUtils.locale("mine.shenqingren2"),//'创建者'
			             TempoUtils.locale("mine.suoshuzhuti"),
			             TempoUtils.locale("mine.suoshugongcheng"),
			             TempoUtils.locale("mine.publish-all-flow-grid-column-createtime"),
			             TempoUtils.locale("mine.shifouyunxufabu")
			             ],//'操作'
			             colModel : [{
			            	 name : 'id',
			            	 index : 'id',
			            	 hidden : true
			             },{
			            	 name : 'servicename',
			            	 index : 'servicename',
			            	 sortable : false
			            	 
			             },
			             {
			            	 name : 'empname',
			            	 index : 'empname',
			            	 sortable : false
			             },{
			            	 name : 'themename',
			            	 index : 'themename',
			            	 sortable : false
			             },
			             {
			            	 name : 'projectname',
			            	 index : 'projectname',
			            	 sortable : false
			             },{
			            	 name : 'apprivodate',
			            	 index : 'apprivodate',
			            	 sortable : false,
			            	 formatter :function(obj, grid, rows, state){
			            		 var time = rows.createtime || rows.apprivodate;
			            		 if(time){
			            			 return DateUtils.formatDateTime(new Date(time))
			            		 }
			            		 return '';
			            	 }
			            	 
			             }, {
			            	 name: "approvestatus", 
			            	 sortable:false,editable : true,
			            		edittype : 'selectmenu',
			            		formatter:function(val, row, rowData) {
			            			return val == "0" ? TempoUtils.locale("mine.exportno") : TempoUtils.locale("mine.exportyes");
			            		},
			            		unformat:function(val, row, rowData) {
			            			return val == TempoUtils.locale("mine.exportyes") ? '1' : '0';
			            		},
			            		editoptions : {
			            			width:"85%",
			            			change : function(e, ui ,rowId) {
			            				var selText = ui.item.label;
			            				var selObj = $(this).find("#" + rowId).find('select').parent().parent();
			            				selObj.attr('title', selText);
			            			},
			            			data : function(){
			            				return [{id:'0',text:TempoUtils.locale("mine.exportno")},
			            				        {id:'1',text:TempoUtils.locale("mine.exportyes")}];
			            			}
			            		},
			            		defaultValue: '1'}
			           ],
			           multiselect:true,
			           toolbar : [{
			   			id:"download",
						text:TempoUtils.locale("mine.tijiao"),
						click:function(e){
							clickHandle();
						}
					}],
			           onSelectRow : function(rowId){
			        	    var lastEditId = ele.datagrid("getLastEditId");
				 			if (rowId == lastEditId){
				 				return;
				 			}
				 			// 编辑时保存上一次的编辑行
				 			ele.datagrid("saveRow", lastEditId);
				 			ele.datagrid("editRow", rowId, true);
			           },
			           pagination : true
		});
	}
	
	var clickHandle = function(){
		var grid = $('#tempo_achieve_service_none_approve_tab');
		var lastEditId = grid.datagrid("getLastEditId");
		$('#tempo_achieve_service_none_approve_tab').datagrid("saveRow", lastEditId);
		var selectRowIds = grid.datagrid("getGridParam","selarrrow");
		if(selectRowIds == null || selectRowIds.length == 0){
			$.alert({msg: TempoUtils.locale('mine.publish_select_item_please')});
			return;
		}
		var allowsId = [];
		var disAllows = [];
		for (var i = 0; i < selectRowIds.length; i++) {
			var rowData = grid.datagrid('getRowData',selectRowIds[i]);
			if(rowData["approvestatus"] == "1"){
				allowsId.push(rowData["id"]);
			}else{
				disAllows.push(rowData["id"]);
			}
		}
		$.ajax({
			dataType:"json",
			type : "post",
			data : {allowsId:allowsId.toString(),disAllows:disAllows.toString()},
			url:contextPath +　"/api/ai/mine/publish/approveService",
			complete:function(res){
				$('#queryForm .queryBtn').trigger('click');
			}
		});
		
	}
	
	//初始化服务列表
	var initSericeGrid2 = function(ele){
		var height = pHeight-155;
		ele.datagrid({
			datatype : "json",
			url : contextPath + '/api/ai/mine/publish/listApproveService?time=' + new Date().getTime(),
			height : height,
			fit:true,
			rownumbers : true,
			mtype : 'POST',
			postData : {status : '1'},
			autowidth:true,
			rownumbers : true,
			shrinkToFit : true, 
			viewrecords : true,
			striped : true,
			colNames : [ 'id', 
			             TempoUtils.locale("mine.publish-mine-service-grid-col-serviceid"),//'服务名称'
			             TempoUtils.locale("mine.shenqingren2"),//'创建者'
			             TempoUtils.locale("mine.suoshuzhuti"),
			             TempoUtils.locale("mine.suoshugongcheng"),
			             TempoUtils.locale("mine.publish-all-flow-grid-column-createtime"),
			             TempoUtils.locale("mine.shenpijieguo"),
			             ],//'操作'
			             colModel : [{
			            	 name : 'id',
			            	 index : 'id',
			            	 hidden : true
			             },{
			            	 name : 'servicename',
			            	 index : 'servicename',
			            	 sortable : false
			             },
			             {
			            	 name : 'empname',
			            	 index : 'empname',
			            	 sortable : false
			             },{
			            	 name : 'themename',
			            	 index : 'themename',
			            	 sortable : false
			             },
			             {
			            	 name : 'projectname',
			            	 index : 'projectname',
			            	 sortable : false
			             },{
			            	 name : 'apprivodate',
			            	 index : 'apprivodate',
			            	 sortable : false,
			            	 formatter :function(obj, grid, rows, state){
			            		 var time = rows.createtime || rows.apprivodate;
			            		 if(time){
			            			 return DateUtils.formatDateTime(new Date(time))
			            		 }
			            		 return '';
			            	 }
			            	 
			             },{
			            	 name : 'approvestatus',
			            	 index : 'approvestatus',
			            	 sortable : false,
			            	 formatter :function(obj, grid, rows, state){
			            		 var approvestatus = rows.approvestatus;
			            		 if(approvestatus == 1){
			            			 return TempoUtils.locale("mine.shenpitongguo");
			            		 }
			            		 return  TempoUtils.locale("mine.shenpibutongguo");
			            	 }
			             }
			           ],
			           pagination : true
		});
	}
	initMainPage();
});	